package br.jcrsistemas.componentes;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import br.jcrsistemas.util.FixedLengthDocument;
import br.jcrsistemas.util.NumberDocument;

public class SysTextField extends JTextField {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private boolean upperCase;
    private boolean apenasNumero;
    private boolean obrigatorio;
    private boolean enterChangeFocus;
    private Component nextComponente;
    private String valorInicial;
    private int tamanho;
    private String tabela;
    private String campo;
    private char tipo;
    private SysDocument documento = new SysDocument();

    public SysTextField() {
        super();
        setSize(getWidth(), 19);
        this.addFocusListener(new ActionFoco());
        setObrigatorio(false);
        setUpperCase(true);
        setInputVerifier(new ValidTextField());
        setEnterChangeFocus(true);
        setDocument(documento);
        addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if(evt.getKeyCode() == KeyEvent.VK_ENTER && enterChangeFocus) {
                    transferFocus();
                }
            }
        });


    }

    public SysTextField(int tamanho) {
        this();
        setDocument(new FixedLengthDocument(tamanho, isUpperCase()));
        this.setTamanho(tamanho);
    }

    public SysTextField(int tamanho, boolean upperCase) {
        this();
        setDocument(new FixedLengthDocument(tamanho, isUpperCase()));
        this.setTamanho(tamanho);
        setUpperCase(upperCase);
    }

    public void setObrigatorio(boolean obrigatorio) {
        this.obrigatorio = obrigatorio;
    }

    public boolean isObrigatorio() {
        return obrigatorio;
    }

    public void setUpperCase(boolean upperCase) {
        this.upperCase = upperCase;
        documento.setUpperCase(upperCase);
//        setDocument(new FixedLengthDocument(tamanho, upperCase));
    }

    public boolean isUpperCase() {
        return upperCase;
    }

    public void setNextComponente(Component nextComponente) {
        this.nextComponente = nextComponente;
    }

    public Component getNextComponente() {
        return nextComponente;
    }

    public void setValorInicial(String valorInicial) {
        this.valorInicial = valorInicial;
    }

    public String getValorInicial() {
        return valorInicial;
    }

    public void setTamanho(int tamanho) {
        this.tamanho = tamanho;
        documento.setMaxLength(tamanho);
//        setDocument(new FixedLengthDocument(tamanho, isUpperCase()));
    }

    public int getTamanho() {
        return tamanho;
    }

    public void setTabela(String tabela) {
        this.tabela = tabela;
    }

    public String getTabela() {
        return tabela;
    }

    public void setCampo(String campo) {
        this.campo = campo;
    }

    public String getCampo() {
        return campo;
    }

    public void setTipo(char tipo) {
        this.tipo = tipo;
    }

    public char getTipo() {
        return tipo;
    }

    public void setApenasNumero(boolean apenasNumero) {
        this.apenasNumero = apenasNumero;
        documento.setApenasNumeros(apenasNumero);
//        if (isApenasNumero()) {
//            setDocument(new NumberDocument(tamanho));
//        } else {
//            setDocument(new FixedLengthDocument(tamanho, isUpperCase()));
//        }
    }

    public boolean isApenasNumero() {
        return apenasNumero;
    }

    public class ActionFoco implements FocusListener {

        @Override
        public void focusGained(FocusEvent arg0) {
            JTextField componente = (JTextField) arg0.getComponent();
            componente.setBackground(new Color(Integer.parseInt("FFE87C", 16)));

        }

        @Override
        public void focusLost(FocusEvent arg0) {
            JTextField componente = (JTextField) arg0.getComponent();

            componente.setBackground(Color.WHITE);
            if (getNextComponente() != null) {
                getNextComponente().requestFocus();
            }

        }
    }

    public boolean isVazio() {
        return getText().isEmpty();
    }
    
    public void setEnterChangeFocus(boolean enterChangeFocus) {
        this.enterChangeFocus = enterChangeFocus;
    }
    
    public boolean isEnterChangeFocus() {
        return this.enterChangeFocus;
    }

    private class ValidTextField extends InputVerifier {

        @Override
        public boolean verify(JComponent input) {
            if (obrigatorio) {
                if (getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Campo obrigatório!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
                    return false;
                }
                return true;
            }
            return true;
        }
    }
}